package servlet.ProductServlet;

import dao.ProductDAO;
import entity.ProductBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import myUtil.productCheck;

import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ProductSaveServlet")
public class ProductSaveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        String productCode = "";
        String productName = "";
        String productSize = "";
        String batch = "";
        String worker = "";
        String processingMethod = "";
        String processingDate = "";
        String workshop = "";
        if (request.getParameter("productCode") != null) productCode = request.getParameter("productCode");
        if (request.getParameter("productName") != null) productName = request.getParameter("productName");
        if (request.getParameter("productSize") != null) productSize = request.getParameter("productSize");
        if (request.getParameter("batch") != null) batch = request.getParameter("batch");
        if (request.getParameter("worker") != null) worker = request.getParameter("worker");
        if (request.getParameter("processingMethod") != null) processingMethod = request.getParameter("processingMethod");
        if (request.getParameter("processingDate") != null) processingDate = request.getParameter("processingDate");
        if (request.getParameter("workshop") != null) workshop = request.getParameter("workshop");
        String id = request.getParameter("id");
        ProductBean productBean = new ProductBean();
        productBean.setProductCode(productCode);
        productBean.setProductName(productName);
        productBean.setBatch(batch);
        productBean.setProductSize(productSize);
        productBean.setWorker(worker);
        productBean.setProcessingMethod(processingMethod);
        productBean.setProcessingDate(processingDate);
        productBean.setWorkshop(workshop);
        if (productCheck.checkCode(productCode) && productCheck.checkS(productName) && productCheck.checkS(productSize) &&
                productCheck.checkBatch(batch) && productCheck.checkS(worker) &&
                productCheck.checkS(processingDate) &&productCheck.checkS(workshop)) {
            ProductDAO dao = new ProductDAO(productCode, productName, batch, worker,
                    processingDate, workshop, productSize, processingMethod);

            if (id == null || id.equals("") || id.equals("0")) {
                dao.addDoProduct();
            }
            else {
                dao.editDoProduct(Integer.parseInt(id));
            }
            response.sendRedirect("ProductListServlet");
        } else {

            request.setAttribute("productBean", productBean);
            request.getRequestDispatcher("product_edit.jsp").forward(request, response);
        }
    }
}

